package rpg.core
{
	import rpg.Utils.GameUtils;
	import rpg.role.Hero;

	public class RushComponent extends FrameComponent
	{
		public var speed : int;
		private var role : Hero;

		private var targetX : int;
		private var targetY : int;

		public function RushComponent(type : * = null)
		{
			super(type ? type : RushComponent);
		}

		override public function notifyAdded() : void
		{
			role = owner as Hero;
			role.isRush = true;
		}

		public function setTargetPosition(targetX : int , targetY : int) : void
		{
			this.targetX = targetX;
			this.targetY = targetY;
		}

		override public function update(delay : uint) : void
		{
			var dist : int = GameUtils.getDistance(role.mapX , role.mapY , targetX , targetY);
			if(dist < speed)
			{
				role.mapX = targetX;
				role.mapY = targetY;
				this.destroy();
				role.isRush = false;
			}
			else
			{
				var rotation : int = GameUtils.getRotation(role.mapX , role.mapY , targetX , targetY);
				var dx : int = GameUtils.cosD(rotation) * speed;
				var dy : int = GameUtils.sinD(rotation) * speed;
				role.mapX += dx;
				role.mapY += dy;
			}
		}
	}
}